package summary;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/9 10:39
 * https://leetcode-cn.com/problems/4sum-ii/
 */
public class Title454 {

    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        int count = 0;
        //key 存放两数之和， value 存放两数出现的次数
        Map<Integer, Integer> hashmap = new HashMap<>();
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                int sum = nums1[i] + nums2[j];
                hashmap.put(sum, hashmap.getOrDefault(sum, 0) + 1);
            }
        }
        for (int i = 0; i < nums3.length; i++) {
            for (int j = 0; j < nums4.length; j++) {
                int sum = nums3[i] + nums4[j];
                Integer integer = hashmap.get(-sum);
                if (integer != null) {
                    count += integer;
                }
            }
        }
        return count;
    }













    public int fourSumCount1(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer, Integer> hash = new HashMap<>();
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                int sum = nums1[i] + nums2[j];
                hash.put(sum, hash.getOrDefault(sum, 0) + 1);
            }
        }
        int count = 0;
        for (int i = 0; i < nums3.length; i++) {
            for (int j = 0; j < nums4.length; j++) {
                int sum = nums3[i] + nums4[j];
                Integer integer = hash.get(sum);
                if (integer != null) {
                    count += integer;
                }
            }
        }
        return count;
    }

    public static void main(String[] args) {

    }
}
